package com.news.controller;


import com.news.model.auto.Role;
import com.news.model.auto.Shudu;
import com.news.service.IRoleService;
import com.news.service.impl.ShuduServiceImpl;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 达瓦里氏
 * @since 2021-12-28
 */
@RestController
@RequestMapping("/shudu")
@CrossOrigin
public class ShuduController {

    @Resource
    private ShuduServiceImpl shuduService;
    /*查询数独*/
    @GetMapping("/getShudu")
    public List<Shudu> getShudu(){
        return shuduService.list();
    }

    /*通过id查询数独*/
    @GetMapping("/getShuduById/{id}")
    public Shudu getShuduById(@PathVariable("id") int id){
        return shuduService.getById(id);
    }

    /*修改数独*/
    @PostMapping("/updateShudu")
    public void updateShudu(@RequestBody Shudu shudu){
        shuduService.updateById(shudu);
    }

    /*添加数独*/
    @PostMapping("/setShudu")
    public void setShudu(@RequestBody Shudu shudu){
        shuduService.save(shudu);
    }

    /*删除数独*/
    @PostMapping("/deletedById/{id}")
    public boolean deleted(@PathVariable("id") int id){
        return shuduService.removeById(id);
    }

    /*图片上传*/
    @PostMapping("/elupload")
    public String elUpload(MultipartFile file){
        String fileName = file.getOriginalFilename();
        System.out.println("进来了");
        File dest = new File("D:\\project\\news\\vue-news\\static\\img\\shudu\\" + fileName);
        try {
            file.transferTo(dest);
            return "上传成功";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传失败！";
    }
}
